


export const resType = {
    dir: 'dir',
    file: 'file'
}



export const g_bundleName = {
    resources: 'resources',
    login: 'login',
    update: 'update',
    home: 'home',
    gameBlock: 'gameBlock',
    gameSnake: 'gameSnake',
    gameGeCao: 'gameGeCao',
    gameMoreMoney: 'gameMoreMoney',
    gameEat: 'gameEat'


}



//大厅 资源常驻 其他资源 随用随删 


export class AssetsMgr {

    private static _instance: AssetsMgr;

    private assets: Record<string, cc.AssetManager.Bundle> = {};

    private assetsIds: Record<string, any> = {};

    public static obj() {

        if (this._instance == null) {
            this._instance = new AssetsMgr();
            this._instance.init();

        }
        return this._instance;
    }

    init() {

    }



    loadBundle(bundleName: any) {
        if (this.assets[bundleName]) {
            return Promise.resolve(this.assets[bundleName]);
        }

        return new Promise<cc.Asset>((res, rej) => {
            cc.assetManager.loadBundle(bundleName, (err: any, bundle: any) => {
                if (err) {
                    console.log(err);
                    rej(err);
                } else {
                    this.assets[bundle.name] = bundle;
                    this.assetsIds[bundle.name] = {};
                    console.log("load bundle success", bundle.name);
                    res(bundle);
                }
            })
        });
    }

    releaseBundle(bundleName: any) {
        if (this.assets[bundleName]) {
            this.assets[bundleName].releaseAll();
            delete this.assets[bundleName];
        }
    }

    releaseRes(bundleName: string, resName: string) {
        if (this.assets[bundleName]) {
            this.assets[bundleName].release(resName);
        }
    }


    async preLoadRes(rType: string, bundleName: string, resName: string): Promise<cc.Asset | cc.Asset[]> {

        if (bundleName == g_bundleName.resources) {
            return await this.preLoadResourcesRes(rType, resName)
        }
        return new Promise<cc.Asset | cc.Asset[]>(async (res, rej) => {
            if (rType == resType.dir) {
                if (!this.assets[bundleName]) {
                    await this.loadBundle(bundleName);
                }
                this.assets[bundleName].loadDir(resName, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
                    console.log("loadDir finish:" + finish + " total:" + total + " item:", item);
                }, (error: Error, items: Array<cc.Asset>) => {
                    if (error) {
                        console.log("loadDir error:" + error);
                        rej(error);
                    } else {
                        console.log("loadDir success", items);
                        for (let id in items) {
                            console.log('items = ', items[id]);
                        }
                        return res(items);
                    }
                });
            } else if (rType == resType.file) {
                if (!this.assets[bundleName]) {
                    await this.loadBundle(bundleName);
                }
                this.assets[bundleName].load(resName, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
                    console.log("load file finish:" + finish + " total:" + total + " item:", item);
                }, (error: Error, items: cc.Asset) => {
                    if (error) {
                        console.log("load file error:", error);
                        rej(error);
                    } else {
                        console.log("load file success:", items);
                    }
                    return res(items);
                });
            }
        });


    }


    async loadRes(bundleName: string, resName: string): Promise<cc.Asset> {
        if (bundleName == g_bundleName.resources) {
            return await this.loadResourecsRes(resName);
        }

        if (!this.assets[bundleName]) {
            await this.loadBundle(bundleName);
        }
        return new Promise<cc.Asset>((res, rej) => {
            this.assets[bundleName].load(resName, (err: Error, assets: any) => {
                if (err) {
                    console.log(err);
                    rej(err);
                } else {
                    console.log("loadRes success", assets.name);
                    res(assets);
                }
            });
        });
    }


    private loadResourecsRes(resName: string): Promise<cc.Asset> {
        return new Promise((res, rej) => {
            cc.resources.load(resName, (err: Error, assets: any) => {
                if (err) {
                    console.log(err);
                    rej(err);
                } else {
                    console.log("loadRes success", assets.name);
                    res(assets);
                }
            });
        });
    }




    private preLoadResourcesRes(rType: string, resName: string): Promise<cc.Asset[] | cc.Asset> {
        return new Promise<cc.Asset | cc.Asset[]>(async (res, rej) => {
            if (rType == resType.dir) {
                cc.resources.loadDir(resName, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
                    console.log("loadDir finish:" + finish + " total:" + total + " item:" + item);
                }, (error: Error, items: Array<cc.Asset>) => {
                    if (error) {
                        console.log("loadDir error:" + error);
                        rej(error);
                    } else {
                        console.log("loadDir success");
                        return res(items);
                    }
                });
            } else if (rType == resType.file) {
                cc.resources.load(resName, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
                    console.log("load file finish:" + finish + " total:" + total + " item:", item);
                }, (error: Error, items: cc.Asset) => {
                    if (error) {
                        console.log("load file error:", error);
                        rej(error);
                    } else {
                        console.log("load file success:", items);
                        return res(items);
                    }
                });
            }
        });
    }



}


